# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name,field_selection_name as selection_name
from openerp.addons.dm_rubylong import get_rubylong_fields_xml,get_rublong_data_url
from openerp.osv import fields, osv
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class stock_picking(osv.osv):
    
    _inherit="stock.picking"
    
    def _get_rubylong_xml_other_print(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        data_xml = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
                        #单据编号
                        'name',
                        #仓库名称
                        ('warehouse_dest_id.name','warehouse_dest_id'),
                        ('id','order_id'),  
                        #领料日期
                        'date_done',                        
                        #打印仓库名称
                        ('warehouse_name_print','warehouse_name'),       
                        ('partner_id.name','partner_id'),
                        ('dept_id.name','dept_id'),
                        ('emp_id.name','emp_id'),   

                        ]
            #创建日期,创建人
            create_date = hasattr(order,'create_date') and (fields.datetime.context_timestamp(cr, uid,
                                                        timestamp=datetime.strptime(order.create_date, DEFAULT_SERVER_DATETIME_FORMAT),
                                                         context=context)) or ''
            create_uid = hasattr(order,'create_uid') and order.create_uid.name or ''
            order_fields += [{'create_date':create_date},{'create_uid':create_uid}]
            #完成日期
#            date_done = fields.datetime.context_timestamp(cr, uid,
#                                                        timestamp=datetime.strptime(order.date_done, DEFAULT_SERVER_DATETIME_FORMAT),
#                                                         context=context)            
#            order_fields += [{'date_done':date_done}]
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            #detail data
            line_fields = [
                        #物料代码
                        ('product_id.default_code','default_code'),
                        #物料名称
                        ('product_id.name','product_id'),
                        #规格
                        ('product_id.variants','variants'),
                        #单位
                        ('product_uom.name','product_uom'),
                        #数量
                        'product_qty',
                        #备注
                        'note',
                        ('product_id.part_no_external','part_no_external'),
                        
                        ('picking_id.id','order_id'),   
                        ]
            order.move_lines.sort(key=lambda x:x.product_id.default_code)
            seq = 0
            for line in order.move_lines:
                seq += 1
                line_fields_new = line_fields + [{'order_line':seq}]
                data_xml += get_rubylong_fields_xml(line, 'detail', line_fields_new)
                
            #write data to file            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)
            
        return res            

    _columns = {
            'rubylong_xml_file_other_out':fields.function(_get_rubylong_xml_other_print, type='text', string='Rubylong xml'),
            }
    

class other_picking(osv.osv):
    
    _inherit="stock.picking.out"
    
    def _get_rubylong_xml_other_print(self, cr, uid, ids, field_names, args, context=None):
        return self.pool.get('stock.picking')._get_rubylong_xml_other_print(cr, uid, ids, field_names, args, context)

    _columns = {
            'rubylong_xml_file_other_out':fields.function(_get_rubylong_xml_other_print, type='text', string='Rubylong xml print'),
            }    
#po_super.STATE_SELECTION = STATE_SELECTION_PO  